package com.wang.controller;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.UUID;

@RestController
public class FileController {


    //方式一

    //@RequestParam("file") 将name=file控件得到的文件封装成CommonsMultipartFile 对象
    //批量上传CommonsMultipartFile则为数组即可
    @RequestMapping("/upload")
    public String upload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {

        //获取上传的文件名
        String uploadFileName=file.getOriginalFilename();
        //判断文件名是否为空
        if(uploadFileName.equals("")){
            return "文件名为空，无法上传";
        }
        //设置服务器中保存的路径
        String uploadFilePath=request.getServletContext().getRealPath("/uploadFile");
        //找到uploadFilePath路径下的file1文件夹，不存在就创建
        File file1=new File(uploadFilePath);
        if(!file1.exists()){
            file1.mkdir();
        }
        //获取上传文件流
        InputStream inputStream=file.getInputStream();
        //创建缓冲区
        byte[] buffer=new byte[1024*1024];
        int len=0;
        //输出  将上传的文件保存在file1文件夹中
        FileOutputStream fileOutputStream=new FileOutputStream(new File(file1,uploadFileName));

        while ((len=inputStream.read(buffer))>0){
            fileOutputStream.write(buffer);
        }
        //关闭流
        fileOutputStream.close();
        inputStream.close();


        return"上传成功";
    }



    //方式二


    @RequestMapping("/upload2")
    public String uploadFile2(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request) throws IOException {

        //设置服务器中的保存路径
        String uploadPath=request.getServletContext().getRealPath("/uploadFile");
        File realFile=new File(uploadPath);
        if(!realFile.exists()){
            realFile.mkdir();
        }
        String UuidPath= UUID.randomUUID().toString();
        File realFile2=new File(uploadPath+"/"+UuidPath+"/"+file.getOriginalFilename());
        if (!realFile2.exists()){
            realFile2.mkdir();
        }
        file.transferTo(realFile2);

        return "上传成功";
    }


    //文件下载

    @RequestMapping("/load")
    public String load(HttpServletRequest request, HttpServletResponse response) throws IOException {


        String path1="E:\\Desktop\\c++\\c++课后题.zip";
        //下载路径
        String path=request.getServletContext().getRealPath("/uploadFile");
        //获取下载的文件名
        String fileName="c++课后题.zip";
        //设置浏览器支持下载文件
        response.setHeader("Content-disposition","attachment;fileName="+ URLEncoder.encode(fileName, "UTF-8"));
        //清除缓存
        response.reset();
        response.setCharacterEncoding("UTF-8"); //字符编码


        //流
       FileInputStream fileInputStream=new FileInputStream(path1);
       byte[] buffer=new byte[1024];
       int len=0;
       ServletOutputStream outputStream=response.getOutputStream();
       while ((len=fileInputStream.read(buffer))>0){
           outputStream.write(buffer);
           outputStream.flush();
       }
       outputStream.close();
       fileInputStream.close();

        return "下载成功";
    }
}
